<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/11 0011
 * Time: 18:02
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "index";
//畜牧列表
if($op=='index'){

	$condition=array();
	if(!empty($_GPC['animal_name'])){
		$animal_name=trim($_GPC['animal_name']);
		$condition['animal_name LIKE']='%'.$animal_name.'%';
	}
	$condition['uniacid']=$uniacid;
	$all=pdo_getall('cqkundian_farm_animal',$condition);
	$count=count($all);
	$pageSize=10;
	$pageIndex=$_GPC['page'] ? intval($_GPC['page']) : $_GPC['page'];
	$pager=pagination($count,$pageSize,$pageIndex);
	$list=pdo_getall('cqkundian_farm_animal',$condition,'','','rank asc',array($pageIndex,$pageSize));
	include $this->template("web/animal/animal_list");
}

//新增修改商品信息
if($op=='animal_list_edit'){
	if(empty($_GPC['id'])){
		include $this->template("web/animal/edit");	
	}else{
		$list=pdo_get('cqkundian_farm_animal',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
		$list['animal_slide']=unserialize($list['animal_slide']);
        $rule=unserialize($list['animal_rule']);
        $list['animal_rule']=implode("\n",$rule);

        $specData=pdo_getall('cqkundian_farm_animal_sku',array('aid'=>$_GPC['id'],'uniacid'=>$uniacid));
        $spec_src=array();
        for($i=0;$i<count($specData);$i++){
            $spec_src[]=$specData[$i]['spec_src'];
        }

		include $this->template("web/animal/animal_edit");
	}
}

//保存畜牧信息
if($op=='animal_list_save'){
	$animal_slide=$_GPC['animal_slide'];
    for($i=0;$i<count($animal_slide);$i++){
        $animal_slide[$i]=tomedia($animal_slide[$i]);
    }
	$insertData=array(
		'animal_name'=>trim($_GPC['animal_name']),
		'animal_src'=>tomedia($_GPC['animal_src']),
		'price'=>$_GPC['price'],
		'price_desc'=>trim($_GPC['price_desc']),
		'animal_num'=>trim($_GPC['animal_num']),
		'rank'=>$_GPC['rank'],
		'uniacid'=>$uniacid,
		'is_putaway'=>$_GPC['is_putaway'],
		'is_recommend'=>$_GPC['is_recommend'],
		'is_open_sku'=>$_GPC['is_open_sku'],
		'animal_desc'=>$_GPC['animal_desc'],
		'adopt_desc'=>$_GPC['adopt_desc'],
		'mature_period'=>$_GPC['mature_period'],
		'count'=>$_GPC['count'],
		'animal_rule'=>serialize(explode("\n",$_GPC['animal_rule'])),
		'animal_slide'=>serialize($animal_slide),
	);
	$insert_res=pdo_insert('cqkundian_farm_animal',$insertData);
	$aid=pdo_insertid();	//获取刚插入的畜牧id

	if($_GPC['is_open_sku']!=1){  //没有规格
        if($insert_res){
            message("操作成功",$this->createWebUrl("animal"));
        }else{
            message("操作失败");
        }
    }else{  //有规格

        $sku_name=$_GPC['sku_name'];
        $item=$_GPC['item'];
        $spec_src=$_GPC['spec_src'];
        $specItem=$_GPC['specItem'];  //规格项
        $itemDataList=array();
        if(count($specItem)>1){
            $merge=array_merge_recursive($item,$sku_name);
            foreach ($merge as $k=>$v) {
                $itemDataList[]=array(
                    'price'=>$v['price'],
                    'count'=>$v['count'],
                    'sku_name'=>$k,
                    'spec_num'=>$v['goods_num'],
                );
            } 
        }else{
            foreach ($item as $k => $v) {
                $itemDataList[]=array(
                    'price'=>$v['price'],
                    'count'=>$v['count'],
                    'sku_name'=>$k,
                    'spec_num'=>$v['goods_num'],
                );
            }
        }
        $res_spec=0;
        for($i=0;$i<count($itemDataList);$i++){
            $itemDataList[$i]['spec_src']=tomedia($spec_src[$i]);
            $itemDataList[$i]['aid']=$aid;
            $itemDataList[$i]['uniacid']=$uniacid;
            $res_spec+=pdo_insert('cqkundian_farm_animal_sku',$itemDataList[$i]);  //插入商品规格
        }
        //修改规格项的goods_id
        $res_spec_item=0;
        for($i=0;$i<count($specItem);$i++){
            $res_spec_item+=pdo_update('cqkundian_farm_animal_spec',array('aid'=>$aid),array('id'=>$specItem[$i],'uniacid'=>$uniacid));
        }
        if($res_spec && $res_spec_item){
            message("操作成功",$this->createWebUrl("animal"));
        }else{
            message("操作失败");
        }
    }
}

//保存修改的畜牧信息
if($op=='animal_list_edit_save'){

	$animal_slide=$_GPC['animal_slide'];
    for($i=0;$i<count($animal_slide);$i++){
        $animal_slide[$i]=tomedia($animal_slide[$i]);
    }
	$insertData=array(
		'animal_name'=>trim($_GPC['animal_name']),
		'animal_src'=>tomedia($_GPC['animal_src']),
		'price'=>$_GPC['price'],
		'price_desc'=>trim($_GPC['price_desc']),
		'animal_num'=>trim($_GPC['animal_num']),
		'rank'=>$_GPC['rank'],
		'uniacid'=>$uniacid,
		'is_putaway'=>$_GPC['is_putaway'],
		'is_recommend'=>$_GPC['is_recommend'],
		'is_open_sku'=>$_GPC['is_open_sku'],
		'animal_desc'=>$_GPC['animal_desc'],
		'adopt_desc'=>$_GPC['adopt_desc'],
		'mature_period'=>$_GPC['mature_period'],
		'count'=>$_GPC['count'],
		'animal_rule'=>serialize(explode("\n",$_GPC['animal_rule'])),
		'animal_slide'=>serialize($animal_slide),
	);
	$aid=$_GPC['id'];
	$animal_up_res=pdo_update('cqkundian_farm_animal',$insertData,array('uniacid'=>$uniacid,'id'=>$aid));
	if($_GPC['is_open_sku']==1){  //开启
        $sku_name=$_GPC['sku_name'];
        $item=$_GPC['item'];
        $spec_src=$_GPC['spec_src'];
        $specItem=$_GPC['specItem'];  //规格项
        $itemDataList=array();
        if(count($specItem)>1){
            $merge=array_merge_recursive($item,$sku_name);
            foreach ($merge as $k=>$v) {
                $itemDataList[]=array(
                    'price'=>$v['price'],
                    'count'=>$v['count'],
                    'sku_name'=>$k,
                    'spec_num'=>$v['goods_num'],
                );
            } 
        }else{
            foreach ($item as $k => $v) {
                $itemDataList[]=array(
                    'price'=>$v['price'],
                    'count'=>$v['count'],
                    'sku_name'=>$k,
                    'spec_num'=>$v['goods_num'],
                );
            }
        }
        $res_spec=0;
        $newSpec=array();
        for($i=0;$i<count($itemDataList);$i++){
            $itemDataList[$i]['spec_src']=tomedia($spec_src[$i]);
            $itemDataList[$i]['aid']=$aid;
            $itemDataList[$i]['uniacid']=$uniacid;
            //判断该规格是否已存在
            $isSpec=pdo_get('cqkundian_farm_animal_sku',array('aid'=>$aid,'sku_name'=>$itemDataList[$i]['sku_name'],'uniacid'=>$uniacid));
            if($isSpec){
                $res_spec+=pdo_update('cqkundian_farm_animal_sku',$itemDataList[$i],array('sku_name'=>$itemDataList[$i]['sku_name'],'aid'=>$aid,'uniacid'=>$uniacid));
            }else{
                $res_spec+=pdo_insert('cqkundian_farm_animal_sku',$itemDataList[$i]);  //插入商品规格
            }
            $newSpecItem=pdo_get('cqkundian_farm_animal_sku',array('sku_name'=>$itemDataList[$i]['sku_name'],'aid'=>$aid,'uniacid'=>$uniacid),array('sku_name'));
            $newSpec[]=$newSpecItem['sku_name'];
        }
        $oldSpecItem=pdo_getall('cqkundian_farm_animal_sku',array('aid'=>$aid,'uniacid'=>$uniacid),array('sku_name'));
        $oldSpec=array();
        for($i=0;$i<count($oldSpecItem);$i++){
            $oldSpec[]=$oldSpecItem[$i]['sku_name'];
        }
        $oldSpecId=array();
        $oldSpecId = array_diff($oldSpec, $newSpec);
        if(!empty($oldSpecId)){
            //删除原来的规格值
            for($i=0;$i<count($oldSpecId);$i++){
                 $del_old=pdo_delete('cqkundian_farm_animal_sku',array('sku_name'=>$oldSpecId[$i],'uniacid'=>$uniacid,'aid'=>$aid));        
            }
        }
        
        //修改规格项的goods_id
        $res_spec_item=0;
        for($i=0;$i<count($specItem);$i++){
            $res_spec_item+=pdo_update('cqkundian_farm_animal_spec',array('aid'=>$aid),array('id'=>$specItem[$i],'uniacid'=>$uniacid));
        }
        if($animal_up_res || $res_spec || $res_spec_item){
            message("修改成功",$this->createWebUrl("animal"));
        }else{
            message("没有修改任何信息");
        }
    }else{      //关闭
        //删除原来的规格信息
        $del_res=pdo_delete('cqkundian_farm_animal_sku',array('aid'=>$aid,'uniacid'=>$uniacid));
        //删除该商品的规格项以及规格值
        $delSpecItem=pdo_getall('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'aid'=>$aid));
        for($i=0;$i<count($delSpecItem);$i++){
            pdo_delete('cqkundian_farm_animal_spec_value',array('spec_id'=>$delSpecItem[$i]['id'],'uniacid'=>$uniacid));
        }
        pdo_delete('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'aid'=>$aid));
        if($animal_up_res || $del_res){
            message('修改成功!',$this->createWebUrl("animal"));
        }else{
            message('没有修改任何信息!');
        }
    }
    die;
}

//改变推荐状态
if($op=='is_recommendChange'){
	$id=$_GPC['id'];
	$res=pdo_update('cqkundian_farm_animal',array('is_recommend'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
	echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//改变上架状态
if($op=='is_putaway_change'){
	$id=$_GPC['id'];
	$res=pdo_update('cqkundian_farm_animal',array('is_putaway'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
	echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;	
}

//删除畜牧信息
if($op=='delete'){
	$aid=$_GPC['id'];
	$res=pdo_delete('cqkundian_farm_animal',array('id'=>$aid,'uniacid'=>$uniacid));
	if($res){
		pdo_delete('cqkundian_farm_animal_sku',array('aid'=>$aid,'uniacid'=>$uniacid));
		$delSpecItem=pdo_getAll('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'aid'=>$aid));
        for($i=0;$i<count($delSpecItem);$i++){
            pdo_delete('cqkundian_farm_animal_spec_value',array('spec_id'=>$delSpecItem[$i]['id'],'uniacid'=>$uniacid));
        }
        pdo_delete('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'aid'=>$aid));
        echo json_encode(array('status'=>1));
	}else{
		echo json_encode(array('status'=>2));
	}
	die;
}

//修改商品信息时初始化获取规格项
if($op=='getEditSpecItem'){
    $request=array();
    $goods_id=$_GPC['goods_id'];    //商品id
    //根据商品获取规格值
    $editSpecVal=pdo_getAll('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'aid'=>$goods_id));
    //根据规格项id获取规格值
    $spec_id=array();
    for($i=0;$i<count($editSpecVal);$i++){
        $specItem=pdo_getAll('cqkundian_farm_animal_spec_value',array('uniacid'=>$uniacid,'spec_id'=>$editSpecVal[$i]['id']));
        $editSpecVal[$i]['specValue']=$specItem;
        $spec_id[]=$editSpecVal[$i]['id'];
    }
    $returnStr=getSpecSku($spec_id,$uniacid);

    $newSkuSpec=$returnStr['newSkuSpec'];
    $goodsSpecVal=array();
    for($j=1;$j<=count($newSkuSpec);$j++){
        $goodsSpecVal[$j-1]=pdo_get('cqkundian_farm_animal_sku',array('sku_name'=>$newSkuSpec[$j],'aid'=>$goods_id,'uniacid'=>$uniacid));
    }
    $request['specVal']=$editSpecVal;
    $request['specItem']= $returnStr['specItem'];
    $request['newSkuSpec']=$returnStr['newSkuSpec'];
    $request['skuSpec']=$returnStr['skuSpec'];
    $request['goodsSpecVal']=$goodsSpecVal;
    echo json_encode($request);die;
}

//添加规格组
if($op=='addSkuItem'){
    $name=$_GPC['sku_name'];
    $res=pdo_insert('cqkundian_farm_animal_spec',array('name'=>$name,'uniacid'=>$uniacid));
	$spec_id=pdo_insertid();
	echo $res ? json_encode(array('code'=>1,'spec_id'=>$spec_id)) : json_encode(array('code'=>2));die;
}

//删除规格项
if($op=='deleteSkuItem'){
	$id=$_GPC['id'];
    $spec_id_str=explode('_', $_GPC['spec_id_str']);
    $returnStr=getSpecSku($spec_id_str,$uniacid);
    $returnStr['code']=1;
	$res=pdo_delete('cqkundian_farm_animal_spec',array('uniacid'=>$uniacid,'id'=>$id));
	echo $res ? json_encode($returnStr) : json_encode(array('code'=>2));die;
}

//添加规格值
if($op=='addSkuVal'){
    $is_type=$_GPC['is_type'];
	$spec_id=$_GPC['specid'];
	$value=$_GPC['spec_value'];
	$spec_id_str=explode('_', $_GPC['spec_id_str']);
	$insertData=array(
		'uniacid'=>$uniacid,
		'spec_id'=>$spec_id,
		'spec_value'=>$value,
	);
	$res=pdo_insert('cqkundian_farm_animal_spec_value',$insertData);  //插入数据
	$spec_val_id=pdo_insertid();
    $returnStr=getSpecSku($spec_id_str,$uniacid);
    if(!empty($_GPC['is_type'])){  //编辑
        $aid=$_GPC['aid'];
        $newSkuSpec=$returnStr['newSkuSpec'];
        $goodsSpecVal=array();
        for($j=1;$j<=count($newSkuSpec);$j++){
            $goodsSpecVal[$j-1]=pdo_get('cqkundian_farm_animal_sku',array('sku_name'=>$newSkuSpec[$j],'aid'=>$aid,'uniacid'=>$uniacid));
        }
        $returnStr['goodsSpecVal']=$goodsSpecVal;
    }
    
    $returnStr['spec_val_id']=$spec_val_id;
    $returnStr['code']=1;
	echo json_encode($returnStr);die;
}

//删除规格值
if($op=='deleteSkuVal'){
	$spec_id=$_GPC['spec_id'];
	$spec_val_id=$_GPC['spec_val_id'];
    $spec_id_str=explode('_', $_GPC['spec_id_str']);  //当前规格项
	$res=pdo_delete("cqkundian_farm_animal_spec_value",array('id'=>$spec_val_id,'uniacid'=>$uniacid));
    $returnStr=getSpecSku($spec_id_str,$uniacid);
    if(!empty($_GPC['is_type'])){  //编辑
        $aid=$_GPC['aid'];
        $specValue=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$aid,'uniacid'=>$uniacid));//获取该商品的所有规格值
        $newSkuSpec=$returnStr['newSkuSpec'];
        $goodsSpecVal=array();
        for($j=1;$j<=count($newSkuSpec);$j++){
            $goodsSpecVal[$j-1]=pdo_get('cqkundian_farm_animal_sku',array('sku_name'=>$newSkuSpec[$j],'aid'=>$aid,'uniacid'=>$uniacid));
        }
        $delete_where['sku_name LIKE']="%".$spec_val_id.'%';
        $delete_where['aid']=$aid;
        $delete_where['uniacid']=$uniacid;
        pdo_delete('cqkundian_farm_animal_sku',$delete_where);
        $returnStr['goodsSpecVal']=$goodsSpecVal;
    }
	echo  $res ?json_encode($returnStr) : json_encode(array('code'=>2));die;
}

//认养订单
if($op=='adopt_order'){
    $all=$user_total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('cqkundian_farm_animal_order')." WHERE uniacid=".$uniacid);
    $pageIndex=$_GPC['page'] ? $_GPC['page'] :1;
    $pageSize=12;
    $pager=pagination($all,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_animal_order',array('uniacid'=>$uniacid),'','','create_time desc',array($pageIndex,$pageSize));

    include $this->template('web/animal/adopt_order');
}

//删除认养订单
if($op=='delete_adopt_order'){
    $res=pdo_delete('cqkundian_farm_animal_order',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//认养订单详细信息
if($op=='adopt_order_detail'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_animal_order',array('uniacid'=>$uniacid,'id'=>$order_id));
    $animalData=pdo_get('cqkundian_farm_animal',array('id'=>$orderData['aid'],'uniacid'=>$uniacid));
    include $this->template('web/animal/adopt_order_detail');
}

/**
 * 获取规格的组合值，当前选中的规格项，规格值对应的详细信息
 * @param  [array] $spec_id [规格项id]
 * @param  [int] $uniacid [当前小程序id]
 * @return [array]          [array]
 */
function getSpecSku($spec_id,$uniacid){
    sort($spec_id);  //将数组排序
    $returnStr=array();
    $specItem=pdo_getall("cqkundian_farm_animal_spec",array('id in'=>$spec_id,'uniacid'=>$uniacid));
    $arr=array();
    for($i=0;$i<count($specItem);$i++) {
        $spec_where['spec_id'] = $specItem[$i]['id'];  //所有的规格项
        $spec_where['uniacid']=$uniacid;
        $specValue = pdo_getall('cqkundian_farm_animal_spec_value', $spec_where, 'id');
        for ($j = 0; $j < count($specValue); $j++) {
            $arr[$specItem[$i]['id']][] = $specValue[$j]['id'];
        }
    }
    //得到组合后的SKU数组
    $newSpec=ok($arr);
    $skuSpec=array();
    for($i=1;$i<count($newSpec)+1;$i++){
        $new_spec_arr=explode(",",$newSpec[$i]);
        $skuSpec[$i-1]=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$new_spec_arr,'uniacid'=>$uniacid));
    }

    $returnStr['specItem']=$specItem;
    $returnStr['newSkuSpec']=$newSpec;
    $returnStr['skuSpec']=$skuSpec;
    return $returnStr;
}

//领养管理
if($op=='adopt_animal'){
    $condition['uniacid']=$uniacid;
    $status=$_GPC['status'];
    if($status){
        $condition['status']=$status;
    }
    $all=pdo_getall('cqkundian_farm_animal_adopt',$condition);
    $count=count($all);
    $pageSize=10;
    $pageIndex=$_GPC['page'] ? intval($_GPC['page']) :1;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_animal_adopt',$condition,'','','create_time desc',array($pageIndex,$pageSize));
    $begin_time=strtotime(date('Y-m-d',time()));
    for($i=0;$i<count($list);$i++){
        $detail=pdo_get('cqkundian_farm_animal',array('id'=>$list[$i]['aid'],'uniacid'=>$uniacid));
        $list[$i]['animal_src']=$detail['animal_src'];
        $list[$i]['animal_name']=$detail['animal_name'];
        $list[$i]['animal_type']=$detail['animal_type'];
        $list[$i]['predict_ripe']=date("Y/m/d",$list[$i]['predict_ripe']);
        $userData=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$list[$i]['uid']));
        $list[$i]['nickname']=$userData['nickname'];
        //更新领养天数
        $day=floor((time()-$list[$i]['today_time'])/60/60/24);
        pdo_update('cqkundian_farm_animal_adopt',array('adopt_day'=>$day),array('id'=>$list[$i]['id'],'uniacid'=>$uniacid,'status'=>array(2,4)));

        $orderData=pdo_get('cqkundian_farm_animal_order',array('id'=>$list[$i]['order_id'],'uniacid'=>$uniacid));
        $list[$i]['username']=$orderData['username'];
        $list[$i]['phone']=$orderData['phone'];
    }
    include $this->template("web/animal/adopt_list");
}

//领养详情
if($op=='adopt_edit'){
    $id=$_GPC['id'];
    $adoptData=pdo_get('cqkundian_farm_animal_adopt',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    $adoptData['predict_ripe']=date("Y-m-d",$adoptData['predict_ripe']);
    $orderData=pdo_get('cqkundian_farm_animal_order',array('id'=>$adoptData['order_id']));

    $animalData=pdo_get('cqkundian_farm_animal',array('id'=>$adoptData['aid'],'uniacid'=>$uniacid));
    $specVal=pdo_get('cqkundian_farm_animal_sku',array('id'=>$orderData['spec_id'],'uniacid'=>$uniacid));
    $spec_id=explode(",", $specVal['sku_name']);
    $spec_val=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$spec_id));
    $specItem=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$orderData['aid'],'uniacid'=>$uniacid));
    for($j=0;$j<count($specItem);$j++){
        for($m=0;$m<count($spec_val);$m++){
            if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
            }
        }
    }
    include $this->template("web/animal/adopt_edit");
}

//修改预期成熟时间
if($op=='changeTime'){
    $adopt_id=$_GPC['adopt_id'];
    $time=$_GPC['time'];
    $predict_ripe=strtotime($time);
    $res=pdo_update('cqkundian_farm_animal_adopt',array('predict_ripe'=>$predict_ripe),array('id'=>$adopt_id,'uniacid'=>$uniacid));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//认养状态改变
if($op=='saveStatus'){
    $status=$_GPC['status'];
    $adopt_id=$_GPC['adopt_id'];

    $update_data['status']=$status;
    if($status==2){
        $update_data['adopt_day']=1;
        $update_data['today_time']=time();
    }

    $res=pdo_update('cqkundian_farm_animal_adopt',$update_data,array('id'=>$adopt_id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//状态跟踪
if($op=='adopt_status_list'){
    $adopt_id=$_GPC['id'];
    $all=pdo_getall('cqkundian_farm_animal_adopt_status',array('uniacid'=>$uniacid,'adopt_id'=>$adopt_id));
    $count=count($all);
    $pageSize=10;
    $pageIndex=$_GPC['page'] ? intval($_GPC['page']) : $_GPC['page'];
    $pager=pagination($count,$pageSize,$pageIndex);
    $list=pdo_getall('cqkundian_farm_animal_adopt_status',array('uniacid'=>$uniacid,'adopt_id'=>$adopt_id),'','','create_time desc',array($pageIndex,$pageSize));
    for ($i=0;$i<count($list);$i++){
        $list[$i]['src']=unserialize($list[$i]['src']);
    }
    include $this->template('web/animal/adopt_status_list');
}
//新增编辑跟踪信息
if($op=='adopt_status_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_animal_adopt_status',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $adopt_id=$list['adopt_id'];
        $list['src']=unserialize($list['src']);
    }else{
        $adopt_id=$_GPC['adopt_id'];
    }
    include $this->template('web/animal/adopt_status_edit');
}
//保存跟踪信息
if($op=='adopt_status_save'){
    $update_data=array(
        'txt'=>$_GPC['txt'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'adopt_id'=>$_GPC['adopt_id'],
    );
    $src=$_GPC['src'];
    for($i=0;$i<count($src);$i++){
        $src[$i]=tomedia($src[$i]);
    }
    $update_data['src']=serialize($src);
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_animal_adopt_status',$update_data);
    }else{
        $res=pdo_update('cqkundian_farm_animal_adopt_status',$update_data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',$this->createWebUrl('animal'));die;
    }else{
        message('操作失败',$this->createWebUrl('animal'));die;
    }
}

//删除跟踪信息
if($op=='delete_status'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_animal_adopt_status',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//畜牧设置
if($op=='adopt_set'){
    $condition=array(
        'ikey'=>array('animal_list_style','animal_name_show'),
        'uniacid'=>$uniacid,
    );
    $list1=pdo_getall('cqkundian_farm_set',$condition);
    $list=array();
    foreach ($list1 as $key => $value) {
        $list[$value['ikey']]=$value['value'];
    }
    include $this->template("web/animal/adopt_set");
}

//畜牧设置保存
if($op=='adopt_set_save'){
    $data=$_POST;
    include 'function.inc.php';
    $res=insertSetData($data,$uniacid);
    if($res){
        message('操作成功',url('site/entry/animal',array('m'=>'kundian_farm','op'=>'adopt_set')));die;
    }else{
        message('操作失败');die;
    }
}


/**
 *  要解决的数学问题    ：算出C(a,1) * C(b, 1) * ... * C(n, 1)的组合情况，其中C(n, 1)代表从n个元素里任意取一个元素
 *
 *  需要进行排列组合的数组
 *  数组说明：该数组是一个二维数组
 *  $CombinList = array(1 => array("Student10", "Student11"),
 *                      2 => array("Student20", "Student21", "Student22"),
 *                      3 => array("Student30"),
 *                      4 => array("Student40", "Student41", "Student42", "Student43"));
 *
 *  计算C(a,1) * C(b, 1) * ... * C(n, 1)的值
 *
 * 
 * @param  [array] $CombinList [description]
 * @return [type]             [description]
 */
function ok($CombinList) {
    $CombineCount = 1;
    foreach ($CombinList as $Key => $Value) {
        $CombineCount *= count($Value);
    }
    $RepeatTime = $CombineCount;
    foreach ($CombinList as $ClassNo => $StudentList) {
        $RepeatTime = $RepeatTime / count($StudentList);
        $StartPosition = 1;
        foreach ($StudentList as $Student) {
            $TempStartPosition = $StartPosition;
            $SpaceCount = $CombineCount / count($StudentList) / $RepeatTime;
            for ($J = 1; $J <= $SpaceCount; $J ++) {
                for ($I = 0; $I < $RepeatTime; $I ++) {
                    $Result[$TempStartPosition + $I][$ClassNo] = $Student;
                }
                $TempStartPosition += $RepeatTime * count($StudentList);
            }
            $StartPosition += $RepeatTime;
        }
    }
    if($Result){
        foreach ($Result as $k => $v) {
            $Result[$k] = implode(',', $v);
        }
    }
    return $Result;
}